package com.lanou3g.k12.web.controller;


import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.lanou3g.k12.entity.Course;
import com.lanou3g.k12.service.CourseService;
import com.lanou3g.k12.utils.K12Result;
import com.lanou3g.k12.utils.K12ResultUtils;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.*;


@RestController  //关于图书的增删改查
public class CourseController {

    @Autowired
    private CourseService courseService;

    /**
     *
     * @param page  分页助手页数参数
     * @param keyword 关键字查询参数
     * @param size 页面条数参数
     * @return
     */
    @GetMapping("/courses")
    //查询到所有课程,返回给前端
    public K12Result findAllCourse(@RequestParam(value = "page",defaultValue = "1") Integer page,
                                   String keyword,
                                   @RequestParam(value = "size",defaultValue = "30") Integer size){
        try{
            Page<Object> objects;
            if (keyword!= null &&!keyword.trim().equals("")){
                objects = PageHelper.startPage(page, size);
                courseService.search(page, keyword, size);
            }else {
                objects = PageHelper.startPage(page, size);
                courseService.findAllCourse(page,size);
            }
            return K12ResultUtils.ok(objects);
        }catch (Exception e){
            e.printStackTrace();
            return K12ResultUtils.error(500);
        }

    }

    /**
     *
     * @param ids 批量删除传过来的id数组
     * @return
     */
    //批量删除
    @DeleteMapping("/courses/{ids}")
    public K12Result deleteSomeCourse(@PathVariable Long[] ids){
        try{
            courseService.deleteSomeCourse(ids);
            return K12ResultUtils.ok();
        }catch (Exception e){
            e.printStackTrace();
            return K12ResultUtils.error(500);
        }
    }

    /**
     *
     * @param id 删除的id
     * @return
     */
    //通过ID删除
    @DeleteMapping("/course/{id}")
    public K12Result deleteSomeCourse(@PathVariable Long id){
        try{
            courseService.deleteOneCourse(id);
            return K12ResultUtils.ok();
        }catch (Exception e){
            e.printStackTrace();
            return K12ResultUtils.error(500);
        }
    }

    /**
     *
     * @param id 点击单一课程明细传过来的id
     * @return
     */
    @GetMapping("/course/{id}")
    //查询单一课程
    public K12Result searchByID(@PathVariable Long id){
        try {
            Course course = courseService.searchByID(id);
            return K12ResultUtils.ok(course);
        }catch (Exception e){
            e.printStackTrace();
            return K12ResultUtils.error(500);
        }
    }
    //添加

    /**
     *
     * @param course 对象接收前台传过来的参数
     * @return
     */
    @PostMapping("/course")
    public K12Result insertCourse(Course course){
        try {
             courseService.insertCourse(course);
             return K12ResultUtils.ok();
        }catch (Exception e){
            e.printStackTrace();
            return K12ResultUtils.error(500);
        }
    }
    //修改

    /**
     *
     * @param id 修改的id
     * @param course 对象睡醒
     * @return
     */
    @PutMapping("/course/{id}")
    public K12Result updateCourse(@PathVariable Long id, Course course) {
        try {
                courseService.updateCourse(id,course);
                return K12ResultUtils.ok();
        }catch (Exception e){
            e.printStackTrace();
            return K12ResultUtils.error(500);
        }

    }
}
